# QInputDialog对话框使用
from PyQt5.QtWidgets import QFontDialog, QLineEdit, QStyle, QFormLayout, QInputDialog, QVBoxLayout, QWidget, \
    QApplication, QHBoxLayout, QDialog, QPushButton, QMainWindow, QGridLayout, QLabel

from PyQt5.QtGui import QIcon, QPixmap, QFont
from PyQt5.QtCore import Qt

import sys


class WindowClass(QWidget):

    def __init__(self, parent=None):

        super(WindowClass, self).__init__(parent)
        layout = QFormLayout()
        self.btnCity = QPushButton()
        self.btnCity.setText("选择城市")
        self.btnSal = QPushButton()
        self.btnSal.setText("输入工资")
        self.btnName = QPushButton()
        self.btnName.setText("输入名字")
        self.btnFont = QPushButton()
        self.btnFont.setText("选择字体样式")

        self.btnCity.clicked.connect(self.city)
        self.btnSal.clicked.connect(self.sal)
        self.btnName.clicked.connect(self.name)
        self.btnFont.clicked.connect(self.font)

        self.editCity = QLineEdit()
        self.editSal = QLineEdit()
        self.editName = QLineEdit()
        self.editCity.setReadOnly(True)
        self.editSal.setReadOnly(True)
        self.editName.setReadOnly(True)

        self.resize(500, 500)
        layout.addRow(self.btnName, self.editName)
        layout.addRow(self.btnSal, self.editSal)
        layout.addRow(self.btnCity, self.editCity)
        layout.addRow(self.btnFont)

        self.setLayout(layout)

    def city(self):
        items = ['北京', '上海', '天津', '石家庄']
        cityDialog = QInputDialog()
        item, ok = cityDialog.getItem(self, "选择城市", "城市列表", items, 0, False)
        print(item, ok)
        if ok and item:
            self.editCity.setText(item)

    def sal(self):
        salDialog = QInputDialog()
        s, ok = salDialog.getDouble(self, "设置工资", "输入工资")
        if ok:
            self.editSal.setText(str(s))

    def name(self):
        nameDialog = QInputDialog()
        t, ok = nameDialog.getText(self, "设置姓名", "输入名字字符串")
        if ok:
            self.editName.setText(str(t))

    # QFontDialog设置第一个QLineEdit文本字体样式
    def font(self):
        fontMsg, ok = QFontDialog.getFont()
        if ok:
            self.editName.setFont(fontMsg)


if __name__ == "__main__":
    app = QApplication(sys.argv)
    win = WindowClass()
    win.show()
    sys.exit(app.exec_())
